package com.luoqy.speedy.modular.api.controller;

import com.alibaba.fastjson.JSON;
import com.luoqy.speedy.core.base.model.Article;
import com.luoqy.speedy.core.cache.CacheServiceImpl;
import com.luoqy.speedy.data.MySql;
import com.luoqy.speedy.util.Result;
import com.luoqy.speedy.util.aop.log.Log;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api2")
public class ApiController {
		@RequestMapping("/pay")
		@ResponseBody
		@Log({ "扫码支付", "聚合支付集成" })
		public String sweepCode(HttpServletRequest request) {
		    String userAgent = request.getHeader("user-agent");
		    if (userAgent != null && userAgent.contains("AlipayClient")) {
		    	System.err.println("支付宝");
		        return "来自支付宝";
		    }else if (userAgent != null && userAgent.contains("MicroMessenger")) {
		    	System.err.println("微信");
		        return "来自微信";
		    }else{
		    	System.err.println("其他");
		        return "未知来源";
		    }
		}
		@RequestMapping("/test")
		@ResponseBody
		@Log({ "调用了接口", "api类型" })
		public Result test(String name) {
			System.err.println(Integer.valueOf(name));
			Result result = new Result();
			result.setMessage("调用成功");
			List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
			Map<String,Object> map=new HashMap<String,Object>();
			map.put("name", "某默默");
			list.add(map);
			return Result.success("成功",list);
		}

	@RequestMapping("/sqlTest1")
	@ResponseBody
	public Result sqlTest1() {
		return Result.success("成功返回了",MySql.SELECTONE("select * from speedy_article limit 1", Article.class));
	}
	@RequestMapping("/sqlTest")
	@ResponseBody
	public Result sqlTest(String id) {
		CacheServiceImpl cache=new CacheServiceImpl();
		Map data= JSON.parseObject(JSON.toJSONString(cache.getCacheDataByKey("HTTP_STATUS")),Map.class);
		return Result.success("成功返回了",data.get("OK"));
	}
}
